<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="企业配额合约" prop="companyName">
        <el-select ref="mySelect" v-model="queryParams.pactId" placeholder="请选择企业配额合约" @change="handleQuery">
          <el-option v-for="item in companyQuotaList" :key="item.id" :label="item.companyName+'_配额:'+item.quota" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="annual">
        <el-input
          v-model="queryParams.annual"
          placeholder="请输入年度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:companyFiling:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:companyFiling:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:companyFiling:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:companyFiling:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyFilingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称名称" width="320" align="center" prop="name" />
      <el-table-column label="省份" width="120" align="center" prop="province" />
      <el-table-column label="年度" width="120" align="center" prop="annual" />
      <el-table-column label="行业" width="120" align="center" prop="industry" />
      <el-table-column label="月份" width="120" align="center" prop="sb1" />
      <el-table-column label="配额合约配额" align="center" prop="pactName" />
      <el-table-column label="填报人" width="120" align="center" prop="createName" />
      <el-table-column label="填报负责人" width="120" align="center" prop="createChargeName" />
      <el-table-column label="排放总量" width="220" align="center" prop="sb2" />
      <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:companyFiling:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:companyFiling:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业排放填报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" height="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-table-column label="" align="center" prop="id" v-if="false"/>
        <el-form-item label="选择企业配额合约" prop="pactId">
          <el-select ref="mySelect" v-model="pact" placeholder="请选择企业配额合约" @change="changePact">
            <el-option v-for="item in companyQuotaList" :key="item.id" :label="item.companyName+'_配额:'+item.quota" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业名称名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称名称" disabled/>
        </el-form-item>
        <el-form-item label="配额" prop="pactName" disabled>
          <el-input v-model="form.pactName" placeholder="请输入配额合约配额" disabled/>
        </el-form-item>
        <el-form-item label="省份" prop="province">
<!--          <el-select v-model="form.province" placeholder="请选择省份">
            <el-option
              v-for="province in provinces"
              :key="province"
              :label="province"
              :value="province">
            </el-option>
          </el-select>-->
          <el-input v-model="form.province" placeholder="请输入省份" disabled/>
        </el-form-item>
        <el-form-item label="行业" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入行业" disabled/>
        </el-form-item>
        <el-form-item label="年度" prop="annual">
        <el-input v-model="form.annual" placeholder="请输入年度" disabled/>
        </el-form-item>
        <el-form-item label="月份" prop="sb1">
          <el-input v-model="form.sb1" placeholder="请输入月份"/>
        </el-form-item>
<!--        <el-form-item label="填报负责人" prop="createChargeName">
          <el-input v-model="form.createChargeName" placeholder="请输入填报负责人" />
        </el-form-item>-->
        <el-form-item label="排放填报">
          <!-- <fileUpload v-model="form.file"/> -->
           <el-button type="text" @click="handleReport(form)">填报</el-button>
        </el-form-item>
        <el-form-item label="填报人" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入填报人" disabled />
        </el-form-item>
        <el-form-item label="排放总量" prop="sb2">
          <el-input v-model="form.sb2" placeholder="请输入排放总量" disabled/>
        </el-form-item>
<!--        <el-form-item label="" prop="sb1">
          <el-input v-model="form.sb1" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="sb2">
          <el-input v-model="form.sb2" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="sb3">
          <el-input v-model="form.sb3" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="sb4">
          <el-input v-model="form.sb4" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="sb5">
          <el-input v-model="form.sb5" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="sb6">
          <el-input v-model="form.sb6" placeholder="请输入" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="排放填报" :visible.sync="reportVisi" width="1600px" height="950px" v-if="reportVisi" append-to-body>
      <dischargeReporting :form="form" ref="dischargeReporting" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="reportVisi = false">取 消</el-button>
        <el-button type="primary" @click="submitReport">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyFiling, getCompanyFiling, delCompanyFiling, addCompanyFiling, updateCompanyFiling } from "@/api/system/companyFiling";
import { listCompanyQuota } from '@/api/busi/companyQuota'
import { getCompany } from "@/api/busi/company";
import dischargeReporting from '@/views/dischargeReporting/index.vue'
export default {
  name: "CompanyFiling",
  components: { dischargeReporting },
  data() {
    return {
      pact: null,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业排放填报表格数据
      companyFilingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      reportVisi: false,
      //省份
      provinces: [
        '北京市', '天津市', '上海市', '重庆市', '河北省', '山西省', '辽宁省', '吉林省', '黑龙江省',
        '江苏省', '浙江省', '安徽省', '浙南省', '江西省', '山东省', '河南省', '湖北省', '湖南省',
        '广东省', '广西壮族自治区', '海南省', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省',
        '甘肃省', '甘南省', '湖南省', '浙江省', '江西省', '山东省', '河南省', '内蒙古自治区', '广东省',
        '香港特别行政区', '澳门特别行政区'
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        industry: undefined,
        province: undefined,
        annual: undefined,
        pactId: undefined,
        pactName: undefined,
        file: undefined,
        createName: undefined,
        createChargeName: undefined,
        sb1: undefined,
        sb2: undefined,
        sb3: undefined,
        sb4: undefined,
        sb5: undefined,
        sb6: undefined
      },
      // 表单参数
      form: {},
      // 企业配额表格数据
      companyQuotaList: [],
      // 表单校验
      rules: {
        name: [
          { required: false, message: "企业名称名称不能为空", trigger: "blur" }
        ],
        industry: [
          { required: false, message: "排放总量不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "省份不能为空", trigger: "blur" }
        ],
        annual: [
          { required: true, message: "年度不能为空", trigger: "blur" }
        ],
        pactId: [
          { required: true, message: "配额合约id不能为空", trigger: "blur" }
        ],
        pactName: [
          { required: false, message: "配额合约配额不能为空", trigger: "blur" }
        ],
        file: [
          { required: false, message: "表单内容不能为空", trigger: "blur" }
        ],
        createName: [
          { required: false, message: "填报人不能为空", trigger: "blur" }
        ],
        createChargeName: [
          { required: false, message: "填报负责人不能为空", trigger: "blur" }
        ],
        sb1: [
          { required: true, message: "月份不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getcompanyQuota();
    this.getList();
  },
  methods: {
    /** 获取省份 */
    getCompanyProvince(id) {
      getCompany(id).then(response => {
        this.form.province = response.data.province;
        this.form.industry = response.data.industry;
      });
    },
    /* 获取当前配额合约 */
    changePact(){
      this.getCompanyProvince(this.pact.companyId)
      this.form.name=this.pact.companyName
      this.form.pactId=this.pact.id
      this.form.pactName=this.pact.quota
      this.form.annual=this.pact.year
    },
    /* 获取配额合约列表*/
    getcompanyQuota() {
      listCompanyQuota().then(response => {
        this.companyQuotaList = response.rows;
      });
    },
    /** 查询企业排放填报列表 */
    getList() {
      this.loading = true;
      listCompanyFiling(this.queryParams).then(response => {
        this.companyFilingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        industry: undefined,
        province: undefined,
        annual: undefined,
        pactId: undefined,
        pactName: undefined,
        file: undefined,
        createTime: undefined,
        updateTime: undefined,
        createName: this.$store.state.user.name,
        createChargeName: undefined,
        sb1: undefined,
        sb2: undefined,
        sb3: undefined,
        sb4: undefined,
        sb5: undefined,
        sb6: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业排放填报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      console.info(row)
      getCompanyFiling(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改企业排放填报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCompanyFiling(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCompanyFiling(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业排放填报编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delCompanyFiling(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
        this.$download.excel('/system/companyFiling/export', this.queryParams);
    },
    // 排放填报
    handleReport(row) {
      this.reportVisi = true
    },
    submitReport() {
      let data = this.$refs.dischargeReporting.getData()

      this.form.file = JSON.stringify(data.data)
      this.$set(this.form, 'sb2', data.total)

      this.reportVisi = false
    }
  }
};
</script>
<style scoped lang="scss">
::v-deep .el-dialog__header {
  border-bottom: 1px solid #dcdfe6;
  padding: 8px;
}

::v-deep .el-dialog__headerbtn {
  top: 13px;
}

::v-deep .el-dialog__body {
  margin: 10px 10px;
  height: calc(100% - 153px);
  padding: 0;
}

::v-deep .el-dialog {
  height: 83%;
}
</style>
